package com.wyzz.hr.exportfile;

import java.io.IOException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;

import com.wyzz.hr.base.Constants;
import com.wyzz.hr.pojo.Empsalarypay;
import com.wyzz.hr.util.MyTools;

import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;

public class ExportSalary extends MutipleSheetExportHandler implements
		Constants {

	public ExportSalary(List results[], List titles[], boolean mySalary) {
		super(results, titles);
		this.mySalary = mySalary;
	}

	protected void buildTitle() throws RowsExceededException, WriteException {
		writableSheets = new WritableSheet[titles.length];
		for (int i = 0; i < titles.length; i++) {
			writableSheets[i] = writableWorkbook.createSheet(titles[i].get(0)
					.toString(), i);
			int sheetTitleLength = titles[i].size() - 1;
			int count = 0;
			if (mySalary) {
				writableSheets[i].addCell(new Label(0, 0, "\u5E74-\u6708",
						titleCellFormat));
				count = 1;
			}
			writableSheets[i].setRowView(0, 500);
			writableSheets[i].setColumnView(2 + count, 15);
			writableSheets[i].setColumnView(6 + count, 15);
			writableSheets[i].setColumnView(7 + count, 20);
			writableSheets[i].setColumnView(8 + count, 15);
			writableSheets[i].setColumnView(11 + count, 15);
			for (int j = 0; j < sheetTitleLength; j++) {
				if (j > 11)
					writableSheets[i].setColumnView(j + count, 20);
				writableSheets[i].addCell(new Label(j + count, 0, titles[i]
						.get(j + 1).toString(), titleCellFormat));
			}

		}

	}

	protected void buildBody() throws RowsExceededException, WriteException,
			IOException {
		int size = results.length;
		String acceptNameArray[] = new String[size];
		int itemsSize[] = new int[size];
		for (int i = 0; i < size; i++) {
			List list = results[i];
			acceptNameArray[i] = titles[i].get(0).toString();
			itemsSize[i] = titles[i].size() - 13;
			for (int j = 0; j < list.size(); j++) {
				writableSheets[i].setRowView(j + 1, 400);
				Empsalarypay pay = (Empsalarypay) list.get(j);
				int count = 0;
				if (mySalary) {
					writableSheets[i].addCell(new Label(0, j + 1, pay
							.getYearAndMonth(), defaultCellFormat));
					count = 1;
				}
				writableSheets[i].addCell(new Label(count, j + 1, pay
						.getEspEmpno().getEmpDistinctNo(), defaultCellFormat));
				writableSheets[i].addCell(new Label(count + 1, j + 1, pay
						.getEspEmpno().getEmpName(), defaultCellFormat));
				writableSheets[i].addCell(new Label(count + 2, j + 1, pay
						.getEspEmpno().getEmpDeptNo().getDepartmentName(),
						defaultCellFormat));
				writableSheets[i].addCell(new Label(count + 3, j + 1, pay
						.getEspEmpno().getEmpPbNo().getPbName(),
						defaultCellFormat));
				writableSheets[i].addCell(new Label(count + 4, j + 1, pay
						.getEspEmpno().getEmpType().getEmptypeName(),
						defaultCellFormat));
				writableSheets[i]
						.addCell(new Label(count + 5, j + 1, pay
								.getEspLocation().getLocationName(),
								defaultCellFormat));
				writableSheets[i].addCell(new Label(count + 6, j + 1,
						getEmpStaus(pay.getEspEmpno().getEmpStatus())
								.toString(), defaultCellFormat));
				writableSheets[i].addCell(new Label(count + 7, j + 1, MyTools
						.vigenere(pay.getEspEmpconfig().getEscBankAccountNo(),
								MyTools.getUpKey(pay.getEspEmpconfig().getId(),
										MyTools.STRING), MyTools.DECRYPT_MODE),
						defaultCellFormat));
				writableSheets[i]
						.addCell(new Label(count + 8, j + 1, pay
								.getEspEmpconfig().getEscBankName(),
								defaultCellFormat));
				writableSheets[i].addCell(new Label(count + 9, j + 1, pay
						.getEspEmpconfig().getEscCostCenter(),
						defaultCellFormat));
				writableSheets[i].addCell(new Label(count + 10, j + 1, pay
						.getEspEmpconfig().getEscJobgrade().getJobGradeName(),
						defaultCellFormat));
				writableSheets[i].addCell(new Label(count + 11, j + 1, pay
						.getEspEsavId().getEsavEsac().getEsacName(),
						defaultCellFormat));
				int itemSize = itemsSize[i] >= 48 ? 48 : itemsSize[i];
				for (int acssist = 0; acssist < itemSize; acssist++)
					try {
						Class ownerClass = pay.getClass();
						int number = acssist + 1;
						Method getMethod = ownerClass.getMethod(
								(new StringBuilder()).append("getEspColumn")
										.append(number).toString(),
								new Class[0]);
						BigDecimal bd = (BigDecimal) getMethod.invoke(pay,
								new Object[0]);
						if (bd == null)
							bd = new BigDecimal(0);
						Number nb = new Number(count + 12 + acssist, j + 1, bd
								.floatValue(), numberCellFormat);
						writableSheets[i].addCell(nb);
					} catch (Exception e) {
						e.printStackTrace();
					}

			}

		}

		writableWorkbook.write();
	}

	public String getEmpStaus(Integer i) {
		switch (i.intValue()) {
		case 0: // '\0'
			return "\u8F9E\u804C";

		case 1: // '\001'
			return "\u5728\u804C";

		case 2: // '\002'
			return "\u505C\u85AA\u7559\u804C";

		case 3: // '\003'
			return "\u9000\u4F11";

		case 4: // '\004'
			return "\u8F9E\u9000";

		case 5: // '\005'
			return "\u5408\u540C\u5230\u671F";

		case 6: // '\006'
			return "\u5176\u4ED6";
		}
		return null;
	}

	private boolean mySalary;
}
